package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("EventhazardDAO")
@Transactional
public class EventhazardDAOImpl extends AbstractJpaDao<Eventhazard> implements EventhazardDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Eventhazard.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public EventhazardDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Eventhazard findEventhazardById(Integer id) throws DataAccessException {

		return findEventhazardById(id, -1, -1);
	}

	@Transactional
	public Eventhazard findEventhazardById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventhazardById", startResult, maxRows, id);
			return (Eventhazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventhazard> findEventhazardByName(String name) throws DataAccessException {

		return findEventhazardByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventhazard> findEventhazardByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventhazardByName", startResult, maxRows, name);
		return new LinkedHashSet<Eventhazard>(query.getResultList());
	}

	@Transactional
	public Set<Eventhazard> findEventhazardByNameContaining(String name) throws DataAccessException {

		return findEventhazardByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventhazard> findEventhazardByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventhazardByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Eventhazard>(query.getResultList());
	}

	@Transactional
	public Set<Eventhazard> findEventhazardByDescription(String description) throws DataAccessException {

		return findEventhazardByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventhazard> findEventhazardByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventhazardByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Eventhazard>(query.getResultList());
	}

	@Transactional
	public Set<Eventhazard> findEventhazardByDescriptionContaining(String description) throws DataAccessException {

		return findEventhazardByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventhazard> findEventhazardByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventhazardByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Eventhazard>(query.getResultList());
	}

	@Transactional
	public Eventhazard findEventhazardByPrimaryKey(Integer id) throws DataAccessException {
		return findEventhazardByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Eventhazard findEventhazardByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventhazardByPrimaryKey", startResult, maxRows, id);
			return (Eventhazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventhazard> findEventhazardsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventhazardsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventhazard> findAllEventhazards() throws DataAccessException {

		return findAllEventhazards(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventhazard> findAllEventhazards(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllEventhazards", startResult, maxRows);
		return new LinkedHashSet<Eventhazard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventhazard> findEventhazardsByEvent(Integer eventId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllEventhazardsByEvent", start, max, eventId);
		return new LinkedHashSet<Eventhazard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventhazard> findEventhazardsByHazard(Integer hazardId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllEventhazardsByHazard", start, max, hazardId);
		return new LinkedHashSet<Eventhazard>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventhazard> findEventhazardByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Eventhazard>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Eventhazard> findEventhazardsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Eventhazard.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Eventhazard> eventhazards = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return eventhazards;
	}

	@Transactional
	public Eventhazard findEventhazardByPhysicalPrimaryKey(Integer eventId, Integer hazardId)
			throws DataAccessException {
		return findEventhazardByPhysicalPrimaryKey(eventId, hazardId, -1, -1);
	}

	@Transactional
	public Eventhazard findEventhazardByPhysicalPrimaryKey(Integer eventId, Integer hazardId, int startResult,
			int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventhazardByPhyscialPrimaryKey", startResult, maxRows, eventId,
					hazardId);
			return (Eventhazard) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Eventhazard entity) {
		return true;
	}
}
